package model

import (
	"gorm.io/gorm"
	"shopping_08_20/globale"
	"time"
)

type User struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:用户ID;primaryKey;not null;" form:"id" json:"id"`    // 用户ID
	Email     string         `gorm:"column:email;type:char(18);comment:用户邮箱;not null;" form:"email" json:"email"`          // 用户邮箱
	Password  string         `gorm:"column:password;type:char(32);comment:用户密码;not null;" form:"password" json:"password"` // 用户密码
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" form:"created_at" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime;default:NULL;" form:"deleted_at" json:"deleted_at"`
}

func (u *User) TableName() string {
	return "user"
}

// 注册
func (u *User) RegisterUser() error {
	return globale.DB.Create(&u).Error
}

// 登陆
func (u *User) LoginUser() error {
	return globale.DB.Where("email = ?", u.Email).Find(&u).Error
}
